package tests;

import common.Utils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import javax.rmi.CORBA.Util;
import java.time.Duration;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Li_yizYa
 * Date: 2025/9/20
 * Time: 19:50
 */
public class UserPage extends Utils {
    public static String url = "http://101.42.27.228:8080/admin.html";
    public UserPage() {
        super(url);
    }

    /**
     * 从后台管理界面进入人员列表
     */
    public void checkUserList01() {
        locationSubpage("//*[@id=\"userList\"]");
        // 检测人员列表标题是否正确
        String title = driver.findElement(By.xpath("/html/body/div/h2")).getText();
        // 检测人员id是否正常显示
        driver.findElement(By.xpath("//*[@id=\"userList\"]/tr[1]/td[1]"));
        // 检测人员姓名是否正常显示
        driver.findElement(By.xpath("//*[@id=\"userList\"]/tr[1]/td[2]"));
        // 检测人员身份是否正常显示
        driver.findElement(By.xpath("//*[@id=\"userList\"]/tr[1]/td[3]"));

        String expectTitle = "人员列表";
        assert expectTitle.equals(title);
    }

    /**
     * 从后台管理界面进入添加人员信息界面
     */
    public void checkCreateUser() {
        locationSubpage("//*[@id=\"register\"]");
        String title = driver.findElement(By.xpath("/html/body/div/h2")).getText();
        driver.findElement(By.xpath("//*[@id=\"name\"]"));
        driver.findElement(By.xpath("//*[@id=\"phoneNumber\"]"));
        driver.findElement(By.xpath("//*[@id=\"mail\"]"));
        String btn = driver.findElement(By.xpath("//*[@id=\"registerForm\"]/button")).getText();
        String expectBtn = "注册";
        String expectTitle = "填写注册信息";

        assert expectTitle.equals(title);
        assert expectBtn.equals(btn);
    }

    // 添加人员信息异常
    /**
     * 用户名、邮箱号、手机号都不填
     */
    public void createFail01() {
        driver.findElement(By.xpath("//*[@id=\"registerForm\"]/button")).click();
        String nameError = driver.findElement(By.xpath("//*[@id=\"name-error\"]")).getText();
        String mailError = driver.findElement(By.xpath("//*[@id=\"mail-error\"]")).getText();
        String phoneNumberError = driver.findElement(By.xpath("//*[@id=\"phoneNumber-error\"]")).getText();

        String expectNameError = "请输入您的姓名";
        String expectMailError = "请输入有效的邮箱地址";
        String exceptPhoneNumberError = "请输入您的手机号";

        assert nameError.equals(expectNameError);
        assert mailError.equals(expectMailError);
        assert phoneNumberError.equals(exceptPhoneNumberError);
    }
    /**
     * 只填用户名
     */
    public void createFail02() {
        driver.findElement(By.xpath("//*[@id=\"name\"]")).sendKeys("张三");
        driver.findElement(By.xpath("//*[@id=\"registerForm\"]/button")).click();

        String mailError = driver.findElement(By.xpath("//*[@id=\"mail-error\"]")).getText();
        String phoneNumberError = driver.findElement(By.xpath("//*[@id=\"phoneNumber-error\"]")).getText();

        String expectMailError = "请输入有效的邮箱地址";
        String exceptPhoneNumberError = "请输入您的手机号";

        assert mailError.equals(expectMailError);
        assert phoneNumberError.equals(exceptPhoneNumberError);
    }

    /**
     * 用户名 + 邮箱号
     */
    public void createFail03() {
        driver.findElement(By.xpath("//*[@id=\"mail\"]")).sendKeys("142516@qq.com");
        driver.findElement(By.xpath("//*[@id=\"registerForm\"]/button")).click();

        String phoneNumberError = driver.findElement(By.xpath("//*[@id=\"phoneNumber-error\"]")).getText();

        String expectMailError = "请输入有效的邮箱地址";
        String exceptPhoneNumberError = "请输入您的手机号";

        assert phoneNumberError.equals(exceptPhoneNumberError);
    }

    public void clear() {
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("document.getElementById('name').value = '';");
        js.executeScript("document.getElementById('mail').value = '';");
        js.executeScript("document.getElementById('phoneNumber').value = '';");
    }

    /**
     * 手机号已存在
     */
    public void createFail04() {
        clear();
        driver.findElement(By.xpath("//*[@id=\"name\"]")).sendKeys("张三");
        driver.findElement(By.xpath("//*[@id=\"mail\"]")).sendKeys("14251625@qq.com");
        driver.findElement(By.xpath("//*[@id=\"phoneNumber\"]")).sendKeys("17612932553");

        driver.findElement(By.xpath("//*[@id=\"registerForm\"]/button")).click();

        // 处理错误的弹窗 --- 警告弹窗
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(2));
        wait.until(ExpectedConditions.alertIsPresent());
        Alert alert = driver.switchTo().alert();
        String actualMsg = alert.getText();
        alert.accept();

        String expectMsg = "注册失败!手机号已被使用";

        assert expectMsg.equals(actualMsg);
    }

    /**
     * 邮箱号已存在
     */
    public void createFail05() {
        clear();
        driver.findElement(By.xpath("//*[@id=\"name\"]")).sendKeys("张三");
        driver.findElement(By.xpath("//*[@id=\"mail\"]")).sendKeys("142516@qq.com");
        driver.findElement(By.xpath("//*[@id=\"phoneNumber\"]")).sendKeys("17612932555");

        driver.findElement(By.xpath("//*[@id=\"registerForm\"]/button")).click();

        // 处理错误的弹窗 --- 警告弹窗
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(2));
        wait.until(ExpectedConditions.alertIsPresent());
        Alert alert = driver.switchTo().alert();
        String actualMsg = alert.getText();
        alert.accept();

        String expectMsg = "注册失败!邮箱已被使用";

        assert expectMsg.equals(actualMsg);
    }

    /**
     * 正常填写人员信息添加成功
     */
    public void createSuccess() {
        clear();
        String name = "赵六";
        driver.findElement(By.xpath("//*[@id=\"name\"]")).sendKeys(name);
        driver.findElement(By.xpath("//*[@id=\"mail\"]")).sendKeys("142516203@qq.com");
        driver.findElement(By.xpath("//*[@id=\"phoneNumber\"]")).sendKeys("17612932556");

        driver.findElement(By.xpath("//*[@id=\"registerForm\"]/button")).click();

        locationSubpage("//*[@id=\"userList\"]");
        String insertName = driver.findElement(By.xpath("//*[@id=\"userList\"]/tr[1]/td[2]")).getText();

        assert insertName.equals(name);
    }
}
